package des.yordan.mihaylov;

/**
 * @author Yordan Mihaylov
 * @version 2011/06/07
 */

public class CustomerArrival extends SimEvent {

	public CustomerArrival(long simulationTime) {
		super(simulationTime);
	}

	@Override
	public void process() {

		long now = simState.getNow();

		if (simState.isServerBusy()) {
			if (simState.getMax_WQL() > simState.getQueueSize()) {

				// customer serviceInitTime = -1 as it is unknown...yet
				simState.enqueueCustomer(new Customer(now));
				simState.waitingQueueLength.count(1.);

			} else {
				// Customer blocked...
				simState.customerBlocked.count(1.);
			}
			// server is idle
		} else {
			// generate service time for the current customer
			long serviceTime = simState.getServiceTime();

			simState.enqueueEvent(new ServiceCompletion(now + serviceTime));

			simState.setCustomerInServer(new Customer(now, now, now
					+ serviceTime));
			simState.calcCustomerStats();

			// set the current time as an end to the idle period
			// calculates automatically the idle time
			simState.setServerBusy(true);
			simState.utilization.setLastSampleTime(now);
		}

		// schedule the next arrival event
		long nextSimTime = now + simState.getArrivalTime();
		simState.enqueueEvent(new CustomerArrival(nextSimTime));
	}
}
